import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        //test01();
        //test02();
        //test03();
        //test04();
        //test05();
        //test06();
        //test07();
        //test08();
        //test09();
        //test10();
        //test11();
        //test12();
        //test13();
        test14();

    }

    private static void test14 () throws IOException {//截图操作
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("https://www.baidu.com/");

        File file=((TakesScreenshot)webDriver).getScreenshotAs (OutputType.FILE);
        //将file储存到磁盘中

        //commoms-io  依赖封装了好用的Java函数
        FileUtils.copyFile (file,new File ("D://截图_24_03_06.png"));

    }

    private static void test13 () throws InterruptedException {//切换窗口=
        //脚本没那么智能，无法自动切换打开的窗口
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("https://www.baidu.com/");
        sleep (3000);
        String cur_handle=webDriver.getWindowHandle ();
        WebElement tar_button=webDriver.findElement (By.cssSelector ("#s-top-left > a:nth-child(1)"));
        tar_button.click ();
        sleep (3000);

        Set<String> all_handles=webDriver.getWindowHandles ();

        String target="";

        //遍历所有的句柄，跳转到最后一个句柄，就是新打开的页面
        for(String temp:all_handles){
            target=temp;
        }

        webDriver.switchTo ().window (target);



    }

    private static void test12 () throws InterruptedException {//鼠标操作
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("https://www.baidu.com/");
        WebElement input= webDriver.findElement (By.cssSelector ("#kw"));
        input.sendKeys ("蛋糕");
        sleep (3000);
        webDriver.findElement (By.cssSelector ("#su")).click ();
        sleep(3000);
        //定位到目标按钮处
        WebElement target= webDriver.findElement (By.cssSelector ("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));

        //鼠标操作
        Actions actions=new Actions (webDriver);
        sleep (3000);
        //要看到执行的效果，使用  perfrom（）
        //contextClick()执行的操作是鼠标右键点击
        actions.moveToElement (target).contextClick ().perform ();
        sleep(3000);





    }

    private static void test11 () throws InterruptedException {//键盘组合按键
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("https://www.baidu.com/");

        WebElement input= webDriver.findElement (By.cssSelector ("#kw"));
        input.sendKeys ("蛋糕");

        webDriver.findElement (By.cssSelector ("#su")).click ();
        sleep (3000);
        //全选
        input.sendKeys (Keys.CONTROL,"A");
        sleep (3000);
        input.sendKeys (Keys.CONTROL,"X");
        sleep (3000);

        //webDriver.get ("https://www.baidu.com/");
        sleep (3000);
        //WebElement input2= webDriver.findElement (By.cssSelector ("#kw"));
        input.sendKeys (Keys.CONTROL,"V");
        webDriver.findElement (By.cssSelector ("#su")).click ();
        sleep (3000);
        //input2.submit ();

    }

    private static void test10 () {//键盘按键
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("https://www.baidu.com/");

        WebElement input= webDriver.findElement (By.cssSelector ("#kw"));
        input.sendKeys ("蛋糕");
        input.sendKeys (Keys.ENTER);
    }

    private static void test09 () throws InterruptedException {//对浏览器的滚动条进行控制
        //执行脚本，进行强制类型转换
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("https://www.baidu.com/");


        WebElement input= webDriver.findElement (By.cssSelector ("#kw"));
        input.sendKeys ("软件开发");
        input.submit ();
        sleep(3000);

        //execute  执行
        //执行脚本，将界面的滚动条拖动到最下面
        ((JavascriptExecutor)webDriver).executeScript ("document.documentElement.scrollTop=10000");
        sleep(3000);
        //滚动到最上面
        ((JavascriptExecutor)webDriver).executeScript ("document.documentElement.scrollTop=0");


    }

    private static void test08 () {
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("https://www.baidu.com");
        //上面的语句等价于
        webDriver.navigate ().to ("https://www.baidu.com");
        //浏览器最大化
        webDriver.manage ().window ().maximize ();
        //设置浏览器大小
        webDriver.manage ().window ().setSize (new Dimension (100,100));
        //浏览器的前进后退
        webDriver.navigate ().forward ();//导航前进
        webDriver.navigate ().back ();//导航控制后退
        //然后获取当前页面的url来判断结果
    }

    private static void test07 () {//获取url
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("https://www.baidu.com");
        String current_url=webDriver.getCurrentUrl ();
        System.out.println (current_url);
        if(current_url.equals ("https://www.baidu.com/")){
            System.out.println ("测试通过");
        }else {
            System.out.println ("测试不通过");
        }

    }

    private static void test06 () {//获取title
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("http://www.baidu.com");
        String title=webDriver.getTitle ();
        if(title.equals ("百度一下，你就知道")){
            System.out.println ("测试成功");
        }else {
            System.out.println ("测试失败");
        }

    }

    private static void test05 () {
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("http://baidu.com");
        //显式等待是等待固定的时间
        WebDriverWait wait=new WebDriverWait (webDriver,3);
        //判断元素是否可以点击
        wait.until (ExpectedConditions.elementToBeClickable (By.cssSelector ("#s-top-left > a:nth-child(4)")));

    }

    private static void test04 () throws InterruptedException {

        //创建浏览器驱动
        WebDriver webDriver=new ChromeDriver ();
        //打开百度首页
        webDriver.get ("http://www.baidu.com");
        //找到搜索框
        WebElement search_input=webDriver.findElement (By.cssSelector ("#kw"));
        //搜索内容
        search_input.sendKeys ("测试开发");
        //找到百度一下并进行点击
        webDriver.findElement (By.cssSelector ("#su")).click ();
        //隐式等待，设置一个最大等待时间，但是实际等不到这么长时间
        //webDriver.manage ().timeouts ().implicitlyWait (5,MINUTES);



        //判断搜索到的内容中是否符合条件
        List<WebElement> search_result=webDriver.findElements (By.xpath ("//font[text()=\"测试开发\"]"));


        //遍历List   List元素的获得要用i

        for (int i = 0; i < search_result.size (); i++) {
            if(search_result.get (i).getText ().equals ("测试开发")){
                System.out.println ("测试通过");
            }else {
                System.out.println ("测试不通过");
            }

        }

    }

    private static void test03 () throws InterruptedException {//测试清空输入框里的内容
        WebDriver webDriver=new ChromeDriver ();
        webDriver.get ("http://www.baidu.com");
        WebElement input=webDriver.findElement (By.cssSelector ("#kw"));
        input.sendKeys ("测试");
        sleep (3000);
        //找到搜索框，清空输入的内容
        input.clear ();
        sleep(3000);
        input.sendKeys ("软件开发");
        WebElement button=webDriver.findElement (By.cssSelector ("#su"));
        button.click ();//submit()提交数据from表单，不在from表单下的无法使用submit
        sleep (3000);

        List<WebElement> search_result=webDriver.findElements (By.xpath ("//font[text()=\"软件开发\"]"));
        //遍历List   List元素的获得要用i

        for (int i = 0; i < search_result.size (); i++) {
            if(search_result.get (i).getText ().equals ("软件开发")){
                System.out.println ("测试通过");
            }else {
                System.out.println ("测试不通过");
            }
        }
        webDriver.quit ();




    }

    private static void test02 () throws InterruptedException {
        //创建浏览器驱动
        WebDriver webDriver=new ChromeDriver ();
        //打开百度首页
        webDriver.get ("http://www.baidu.com");
        //找到搜索框
        WebElement search_input=webDriver.findElement (By.cssSelector ("#kw"));
        //搜索内容
        search_input.sendKeys ("测试开发");

        WebElement click_button =webDriver.findElement (By.cssSelector ("#su"));
        click_button.submit ();


        //找到百度一下并进行点击
        //webDriver.findElement (By.cssSelector ("#su")).click ();
        sleep(3000);
        //判断搜索到的内容中是否符合条件
        List<WebElement> search_result=webDriver.findElements (By.xpath ("//font[text()=\"测试开发\"]"));
        //遍历List   List元素的获得要用i

        for (int i = 0; i < search_result.size (); i++) {
            if(search_result.get (i).getText ().equals ("测试开发")){
                System.out.println ("测试通过");
                System.out.println ("测试不通过");
            }else {
            }

        }
        webDriver.quit ();

    }

    private static void test01 () {
        //创建一个驱动
        WebDriver webDriver=new ChromeDriver ();
        //打开百度网页
        webDriver.get ("http://www.baidu.com");
        //定位百度搜索框
        //WebElement search_input= webDriver.findElement (By.cssSelector ("#kw"));
        //WebElement search_input= webDriver.findElement (By.xpath ("//*[@id=\"kw\"]"));
        WebElement search_input= webDriver.findElement (By.tagName ("input"));
        //测试
        if(search_input==null){
            System.out.println ("测试失败");
        }else {
            System.out.println ("测试成功");
        }

        //webDriver.quit ();//关闭浏览器会清楚cookie
        webDriver.close ();
    }




}
